package com.ning.parking.controller;


import cn.hutool.core.date.BetweenFormatter;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ning.parking.config.GlobalVariableUtils;
import com.ning.parking.domain.ParkingLotEmpApplyCar;
import com.ning.parking.domain.ParkingRecord;
import com.ning.parking.domain.ParkingUserInfo;
import com.ning.parking.domain.VO.Pagination;
import com.ning.parking.domain.dto.ParkingLotEmpApplyCarDto;
import com.ning.parking.domain.dto.ParkingUserInfoDto;
import com.ning.parking.service.ParkingLotEmpApplyCarService;
import com.ning.parking.service.ParkingUserInfoService;
import com.scsonic.framework.common.response.R;
import com.scsonic.framework.exception.BizException;
import com.scsonic.framework.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("parking")
public class LogindController {

    @Autowired
    private ParkingUserInfoService loginService;

    @Autowired
    private ParkingLotEmpApplyCarService parkingLotEmpApplyCarService;

    /**
     * 登入
     * @param parkingUserInfo
     * @return
     */
    @PostMapping("login")
    public R<String> login(ParkingUserInfo parkingUserInfo) {
        return R.ok(loginService.login(parkingUserInfo));
    }

    @PostMapping("logout")
    public R<String> logout(String token) {
        loginService.logout(token);
        return R.ok();
    }

    @PostMapping("saveUser")
    public R<Long> saveUser(@RequestBody ParkingUserInfo parkingUserInfo) {
        ParkingUserInfo parkingUserInfo1 = loginService.saveUser(parkingUserInfo);
        return R.ok(parkingUserInfo1.getUserId());
    }

    @GetMapping("byId")
    public R<ParkingUserInfo> byId(ParkingUserInfo parkingUserInfo) {
        Long userId = parkingUserInfo.getUserId();
        if (userId == null && parkingUserInfo.getToken() == null) {
            throw new BizException("UserId is null ");
        }
        ParkingUserInfo byId = null;
        if (userId != null){
             byId = loginService.getById(userId);
        }else {
            List<ParkingUserInfo> list = loginService.lambdaQuery().eq(ParkingUserInfo::getToken, parkingUserInfo.getToken()).list();
            if (list!=null&&list.size()>0){
                byId = list.get(0);
            }
        }

        return R.ok(byId);
    }
    @GetMapping("user-page")
    public R<Pagination<ParkingUserInfo>> userPage(ParkingUserInfoDto parkingUserInfo) {
        return R.ok(loginService.userPage(parkingUserInfo));
    }

    @PostMapping("updUser")
    public R<Boolean> updUser(@RequestBody ParkingUserInfo parkingUserInfo) {
        if (parkingUserInfo.getUserId() == null){
            throw new BizException("UserId is null ");
        }
        String password = parkingUserInfo.getPassword();
        if (ObjectUtil.isNotEmpty(password)){
            parkingUserInfo.setPassword(UUID.nameUUIDFromBytes(password.getBytes()).toString());
        }
        boolean updateById = loginService.updateById(parkingUserInfo);
        return R.ok(updateById);
    }

    /**
     * 绑定车牌
     * @param parkingLotEmpApplyCar
     * @return
     */
    @PostMapping("binding-car")
    public R<Boolean> bindingCar(@RequestBody ParkingLotEmpApplyCar parkingLotEmpApplyCar) {
        if (ObjectUtil.isEmpty(parkingLotEmpApplyCar.getOpenId())) {
            throw new BizException("openid is null");
        }

        if (ObjectUtil.isEmpty(parkingLotEmpApplyCar.getCarLicense())) {
            throw new BizException("carLicense is null");
        }
        DateTime date = DateUtils.date();
        parkingLotEmpApplyCar.setUpdateTime(date);
        parkingLotEmpApplyCar.setUpdateBy(GlobalVariableUtils.getUser().getUserName());
        parkingLotEmpApplyCar.setCreateBy(GlobalVariableUtils.getUser().getUserName());
        parkingLotEmpApplyCar.setCreateTime(date);
        boolean save = parkingLotEmpApplyCarService.save(parkingLotEmpApplyCar);

        return R.ok(save);
    }

    /**
     * 绑定车牌
     * @param parkingLotEmpApplyCar
     * @return
     */
    @GetMapping("del-car")
    public R<Boolean> delCar(ParkingLotEmpApplyCar parkingLotEmpApplyCar) {
        if (ObjectUtil.isEmpty(parkingLotEmpApplyCar.getCarId())) {
            throw new BizException("CarId is null");
        }
        boolean save = parkingLotEmpApplyCarService.removeById(parkingLotEmpApplyCar.getCarId());
        return R.ok(save);
    }

    @GetMapping("find-my-car")
    public R< List<ParkingLotEmpApplyCar>> findMyCar(ParkingLotEmpApplyCar parkingLotEmpApplyCar) {
        if (ObjectUtil.isEmpty(parkingLotEmpApplyCar.getOpenId())) {
            throw new BizException("openid is null");
        }
        List<ParkingLotEmpApplyCar> list = parkingLotEmpApplyCarService.lambdaQuery().eq(ParkingLotEmpApplyCar::getOpenId, parkingLotEmpApplyCar.getOpenId()).orderByDesc(ParkingLotEmpApplyCar::getCreateTime).list();
        return R.ok(list);
    }

    /**
     *
     * @param parkingLotEmpApplyCar
     * @return
     */
    @GetMapping("find-my-record")
    public R<Pagination<ParkingRecord>> findMyRecord(ParkingLotEmpApplyCarDto parkingLotEmpApplyCar) {
        if (ObjectUtil.isEmpty(parkingLotEmpApplyCar.getOpenId())) {
            throw new BizException("openid is null");
        }
        Pagination<ParkingRecord> list = parkingLotEmpApplyCarService.findMyRecord(parkingLotEmpApplyCar);

        for (ParkingRecord record : list.getRecords()) {
            if (record.getExitTime() == null && record.getEntTime() != null) {
                record.setParkingTime( DateUtil.formatBetween(  DateUtils.date(),record.getEntTime() , BetweenFormatter.Level.SECOND));
            }else if (record.getExitTime() != null && record.getEntTime() != null) {
                record.setParkingTime( DateUtil.formatBetween( record.getExitTime(), record.getEntTime(), BetweenFormatter.Level.SECOND));
            }
        }
        return R.ok(list);
    }


}
